#include "StdAfx.h"
#include "ILaplacian.h"
//OpenCV libs
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv/cv.h"

const char * const ILaplacian::FunctionNameILaplacian = "Laplacian";

ILaplacian::ILaplacian(void) 
{
	_fnApi.SetFunctionName(ILaplacian::FunctionNameILaplacian);
	_fnApi.SetFunctionDescription("Calculates the Laplacian of an image");
									//inputs
	_fnApi.AddParamDef(new ProcParamDefInt(0, "ddepth", "The desired depth of the destination image. If it is negative, it will be the same as src.depth()", ProcParamDef::input, 0, 500 ));
	_fnApi.AddParamDef(new ProcParamDefInt(1, "ksize", "The aperture size used to compute the second-derivative filters, see getDerivKernels() . It must be positive and odd", ProcParamDef::input, 0, 500 ));
	_fnApi.AddParamDef(new ProcParamDefInt(2, "scale ", "The optional scale factor for the computed Laplacian values (by default, no scaling is applied, see getDerivKernels() )", ProcParamDef::input, -500, 500 ));
	_fnApi.AddParamDef(new ProcParamDefDouble(3, "delta", "The optional value added to the filtered pixels before storing them in destination image", ProcParamDef::input, 0, 500 ));
	//_fnApi.AddParamDef(new ProcParamDefInt(4, "borderType ", "The pixel extrapolation method; see borderInterpolate()", ProcParamDef::input, 0, 500 ));

	//outputs
	_fnApi.AddParamDef(new ProcParamDefInt(0, ProcParam::attribOutputParamNameResult, "0 is success", ProcParamDef::output, 0, 1));

	_fnApi.Validate();
}


ILaplacian::~ILaplacian(void)
{

}


bool ILaplacian::Process(ImageProcStep *step, bool DebugMode)
{	
	bool rc = false;
	if ((step != '\0') && (step->IsValid()))
	{
		
		ProcParamInt *depth = dynamic_cast<ProcParamInt *>(step->GetInputs()->GetParam(0));	//Check the Param number
		ProcParamInt *ksize = dynamic_cast<ProcParamInt *>(step->GetInputs()->GetParam(1));
		ProcParamDouble *scale = dynamic_cast<ProcParamDouble *>(step->GetInputs()->GetParam(2));
		ProcParamDouble *delta = dynamic_cast<ProcParamDouble *>(step->GetInputs()->GetParam(3));
		//ProcParamInt *anchorY = dynamic_cast<borderType *>(step->GetInputs()->GetParam(4));	//Check the type and check the DefType
		if  (depth != '\0' && ksize != '\0' && scale != '\0' && delta != '\0')
		{
			cv::Mat output;
			cv::Mat input = cv::imread(step->GetIntermediateImagePathFilename());
			
			cv::Laplacian(input, output, int(depth->GetValue()), int(ksize->GetValue()), double(scale->GetValue()), double(delta->GetValue()));

			cv::imwrite(step->GetIntermediateImagePathFilename(), output);

			input.release();
			//result->SetValue(0);
			rc = true;
		}
	}
	return rc;
}
